package com.branches.chapeter_4;

import java.util.concurrent.TimeUnit;

public class Factory implements Runnable {

    private Product p;

    public Factory(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        while(true) {
            synchronized (this.p) {
                if (this.p.getCount() > 10) {
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    while(this.p.getCount() < 10) {
                        this.p.add();
                    }
                    this.p.notifyAll();
                }
            }
        }
    }
}
